package com.usian.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.feign.ArticleFeign;
import com.usian.feign.WeMediaFeign;
import com.usian.mapper.ApUserMapper;
import com.usian.mapper.ApUserRealnameMapper;
import com.usian.model.article.pojos.ApAuthor;
import com.usian.model.common.dtos.PageResponseResult;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.media.pojos.WmUser;
import com.usian.model.user.dtos.AuthDto;
import com.usian.model.user.pojos.ApUser;
import com.usian.model.user.pojos.ApUserRealname;
import com.usian.service.ApUserRealnameService;
import io.seata.spring.annotation.GlobalTransactional;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class ApUserRealnameServiceImpl extends ServiceImpl<ApUserRealnameMapper, ApUserRealname> implements ApUserRealnameService {
    public static final Short PASS_AUTH = 9; //审核通过
    public static final Short FAIL_AUTH = 2; //审核失败
    public static final Integer WM_USER=2; //自媒体平台用户
    @Autowired
    private WeMediaFeign weMediaFeign;
    @Autowired
    private ArticleFeign articleFeign;
    @Autowired
    private ApUserMapper apUserMapper;
    @Override
    public PageResponseResult loadListByStatus(AuthDto dto) {
        if (dto == null) {
            return (PageResponseResult) ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //检查参数
        dto.checkParam();
        QueryWrapper<ApUserRealname> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(dto.getStatus().toString())){
            queryWrapper.lambda().eq(ApUserRealname::getStatus,dto.getStatus());
        }
        Page  pageparam=new Page(dto.getPage(),dto.getSize());
        IPage page = page(pageparam, queryWrapper);
        PageResponseResult responseResult = new PageResponseResult(dto.getPage(),dto.getSize(),(int)page.getTotal());
        responseResult.setCode(0);
        responseResult.setData(page.getRecords());
        return responseResult;
    }



    /**
     * 普通用户审核成为自媒体人
     * @param dto
     * @param
     * @return
     */
    @Override
    @GlobalTransactional //seata 全局分布式事务统一管理
    public ResponseResult updateStatusById(AuthDto dto) {
        //1.检查参数
        if (dto == null || dto.getId()==null || dto.getStatus() ==null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //2.更新ap_user_realname 状态改为
        ApUserRealname realname = new ApUserRealname();
        realname.setId(dto.getId());
        realname.setStatus(dto.getStatus());
        if (dto.getMsg()!=null||dto.getMsg()!=""){
                realname.setReason(dto.getMsg());
        }
        updateById(realname);
        //3.status 9  feign wemedia 添加一条
        if (dto.getStatus().equals(PASS_AUTH)){
            //wemedia插入数据
            ApUserRealname userRealname = this.getById(dto.getId());
            ApUser apUser = apUserMapper.selectById(userRealname.getUserId());
            if(apUser==null){
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
            }

            WmUser wmUser = weMediaFeign.findByName(apUser.getName());
            if (wmUser == null){
                wmUser = new WmUser();
                //设置ApUserId
                wmUser.setApUserId(apUser.getId());
                wmUser.setCreatedTime(new Date());
                wmUser.setSalt(apUser.getSalt());
                wmUser.setName(apUser.getName());
                wmUser.setPassword(apUser.getPassword());
                wmUser.setStatus(PASS_AUTH.intValue());
                wmUser.setPhone(apUser.getPhone());
                weMediaFeign.save(wmUser);
            }
        //4.status 9 feign article ap_author 添加数据
            ApAuthor author = articleFeign.findByUserId(wmUser.getApUserId());
            if (author==null){
                author = new ApAuthor();
                author.setName(wmUser.getName());
                author.setType(WM_USER);
                author.setCreatedTime(new Date());
                author.setWmUserId(wmUser.getId());
                articleFeign.save(author);
            }
        }

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
